package com.qingmi.farmer.ui.news;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.model.News;
import com.qingmi.farmer.data.repository.data.OtherDataRepository;
import com.qingmi.farmer.data.request.GetNewsListReq;
import com.qingmi.farmer.databinding.ActivityNewsListBinding;
import com.wjjun.base.ui.base.ListActivity;
import com.qingmi.farmer.ui.webview.WebViewActivity;
import com.qingmi.farmer.viewmodel.NewsItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * 新闻列表
 */
public class NewsListActivity extends ListActivity<NewsListViewModel, ActivityNewsListBinding> {

    public static void start(Context context) {
        Intent intent = new Intent(context, NewsListActivity.class);
        context.startActivity(intent);
    }


    @Inject
    public OtherDataRepository otherDataRepository;

    @Override
    public int getLayoutId() {
        return R.layout.activity_news_list;
    }

    @Override
    protected NewsListViewModel initViewModel() {
        return new NewsListViewModel();
    }

    @Override
    protected void initContent() {
        otherDataRepository.bind(this);
        super.initContent();
        viewModel.setTitle(getString(R.string.title_news));
    }

    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof NewsItemViewModel) {   //新闻
            if (((NewsItemViewModel) item).imgs.size() >= 3) {    //图片数量大于3
                itemBinding.set(BR.viewModel, R.layout.layout_item_news_multi_img).bindExtra(BR.presenter, viewGroupPresenter);
            } else {
                itemBinding.set(BR.viewModel, R.layout.layout_item_news).bindExtra(BR.presenter, viewGroupPresenter);
            }
        }
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel item) {
        if (item instanceof NewsItemViewModel) {
            WebViewActivity.start(NewsListActivity.this, ((NewsItemViewModel) item).url, ((NewsItemViewModel) item).title.get());
        }
    }

    @Override
    public void refreshData(boolean init) {
        super.refreshData(init);
        GetNewsListReq req = new GetNewsListReq();
        otherDataRepository.getNewsList(new DefaultObserver<List<News>>() {
            @Override
            public void onNext(List<News> news) {
                List<NewsItemViewModel> list = new ArrayList<>();
                if (news != null && !news.isEmpty()) {
                    for (News item : news) {
                        list.add(new NewsItemViewModel(item));
                    }
                }
                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                setError(e.getMessage());
            }
        }, req);

    }


    @Override
    public void loadData() {
        super.loadData();
        GetNewsListReq req = new GetNewsListReq();
        req.setNewsId(((NewsItemViewModel) commListViewModel.getLastItem()).id);
        otherDataRepository.getNewsList(new DefaultObserver<List<News>>() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void onNext(List<News> news) {
                List<NewsItemViewModel> list = new ArrayList<>();
                if (news != null && news.isEmpty()) {
                    for (News item : news) {
                        list.add(new NewsItemViewModel(item));
                    }
                    addData(list);
                }
            }

            @Override
            public void onError(MyException e) {
                finishLoadMoreError();
            }
        }, req);
    }
}